﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;


namespace EpisodeWizard.App.WizardPages
{
	public partial class WizardPageRename : CoreUI.WizardPage
	{
		public WizardPageRename()
		{
			InitializeComponent();
		}

		private void WizardPageRename_LeavePage()
		{
			int NbErrors = 0;
			string Errors = "";

			foreach (Core.EpisodeFile EF in Core.Program.GetInstance().EpisodeFiles)
			{
				if (EF.HasAllInformation())
				{
					string NewPath = Path.Combine(Path.GetDirectoryName(EF.Filepath), EF.NewFilename) + Path.GetExtension(EF.Filepath);
					try
					{
						File.Move(EF.Filepath, NewPath);
					}
					catch (Exception Ex)
					{
						Errors += Environment.NewLine + "- " + EF.PossibleShows.SelectedValue.ShowName + " " + EF.PossibleEpisodes.SelectedValue + " : " + Ex.Message;
						++NbErrors;
					}
				}
			}

			if (NbErrors > 0)
			{
				MessageBox.Show("There were " + NbErrors + " errors renaming the files. Maybe you moved or renamed the files since you added them in EpisodeWizard"
						+ Environment.NewLine + Errors, "Errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
		}
	}
}
